home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / utility.lha / utility / strings.H < prev   
C/C++ Source or Header  |  1993-08-08  |  4KB  |  137 lines

  1. // This is a simple string package for C++.  Strings have automatic
  2. // storage management and value semantics.  Copying of the string
  3. // contents is common in this implementation.
  4. //
  5. // The string object has a small internal buffer that is used for short
  6. // strings.  This usually saves a great number memory allocations (in
  7. // one test 80% of all strings were stored in the local buffer).
  8. //
  9. // This header file also declares a list of strings, LIST(String),
  10. // a string iterator, ITERATOR(String), and output on i/o streams.
  11. //
  12. // .SS History
  13. // Author: Dag Bruck.
  14. //
  15. // $Id: strings.H,v 1.3 91/08/28 11:33:40 dag Exp $
  16.  
  17.  
  18. #ifndef STRINGS_H
  19. #define STRINGS_H
  20.  
  21.  
  22. #include <defs.H>
  23. #include <list.H>
  24. #include <listiter.H>
  25. #include <iostream.h>
  26. #include <string.h>
  27.  
  28.  
  29. class String {
  30. public:
  31.   String();
  32.   // Constructs a string of zero length.
  33.   
  34.   String(const char *);
  35.   // Constructs a string from a character array.
  36.  
  37.   String(const String &);
  38.   // Constructs a copy of another string.
  39.   
  40.   ~String();
  41.   // Destroys string, deallocating storage.
  42.  
  43.   String& operator = (const String &);
  44.   String& operator = (const char *);
  45.   String& operator = (char);
  46.   // Assignment to string.
  47.  
  48.   void operator += (const String &);
  49.   void operator += (char);
  50.   // Appends a string or character to a string.
  51.  
  52.   char operator () (unsigned i) const;
  53.   // Returns i'th character of string.  Strings start at 0 (zero).
  54.   // Returns zero if index if out of bounds.
  55.  
  56.   String operator () (unsigned start, unsigned n) const;
  57.   // Returns the substring starting at `start' which is at
  58.   // most `n' characters long.  Strings start at 0 (zero).
  59.  
  60.   operator const char * () const;
  61.   // Returns pointer to contents of string.
  62.  
  63.   char* Copy() const;
  64.   // Returns a copy of the string contents.  The copy is
  65.   // allocated with new and must be deleted by the caller.
  66.  
  67.   String upper() const;
  68.   String lower() const;
  69.   // Returns a string where every lower (upper) case letter is converted
  70.   // to upper (lower) case, according to ctype(3).  Other characters are
  71.   // copied unchanged.
  72.   
  73.   unsigned Length() const;
  74.   // Returns length of string (excluding terminating null).
  75.  
  76.   unsigned Size() const;
  77.   // Returns number of bytes allocated for string contents.
  78.  
  79.   unsigned Hashval() const;
  80.   // Returns an unsigned integer suitable as index in hashtables, etc.
  81.  
  82. private:
  83.   char* str;
  84.   // Pointer to allocated storage.
  85.  
  86.   unsigned size;
  87.   // Size of allocated storage for string (bytes).
  88.  
  89.   enum {internal_buffer_size = 8};
  90.   char buf[internal_buffer_size];
  91.   // Internal buffer for short strings; saves free store allocations.
  92.  
  93.   friend ostream& operator << (ostream &, const String &);
  94.   // Inserts a string in the output stream.  The same formatting
  95.   // as for char* takes place.
  96.  
  97.   friend istream& operator >> (istream &, String &);
  98.   // Extracts a string from an input stream.  Initial whitespace
  99.   // is skipped.  The string extends until the next whitespace.
  100.  
  101.   friend boolean operator == (const String &, const String &);
  102.   friend boolean operator == (const String &, const char *);
  103.   friend boolean operator == (char *, const String &);
  104.   // Tests for equality ("not equal" also defined).
  105. };
  106.  
  107.  
  108. String operator + (const String &, const String &);
  109. // Concatenates two strings.
  110.  
  111. inline unsigned String :: Size() const
  112. { return size; }
  113.  
  114. inline unsigned String :: Length() const
  115. { return strlen(str); }
  116.  
  117. inline String :: operator const char * () const
  118. { return str; }
  119.  
  120. inline boolean operator == (const String& s1, const String& s2)
  121. { return s1.str[0] == s2.str[0] && strcmp(s1.str, s2.str) == 0; }
  122.  
  123. inline boolean operator != (const String& s1, const String& s2)
  124. { return !(s1 == s2); }
  125.  
  126. inline boolean operator != (const String& s1, const char* s2)
  127. { return !(s1 == s2); }
  128.  
  129. inline boolean operator != (char* s1, const String& s2)
  130. { return !(s1 == s2); }
  131.  
  132. LIST_DECLARE(String);
  133. ITERATOR_DECLARE(String);
  134.  
  135.  
  136. #endif
  137.